package com.starlink.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starlink.common.PageInfoTwo;
import com.starlink.common.utils.AssertUtil;
import com.starlink.context.SessionContext;
import com.starlink.model.dtomapper.ShoppingCartDtoMapper;
import com.starlink.model.domain.ShoppingCartDO;
import com.starlink.mapper.ShoppingCartMapper;
import com.starlink.model.request.ShoppingCartRequest;
import com.starlink.model.util.ShoppingCartConvert;
import com.starlink.service.ShoppingCartService;
import com.starlink.model.vo.ShoppingCartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.starlink.utils.IDUtils.getId;

/**
 * @author zhanghong
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCartDO> implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public ShoppingCartVO getByKey(long id) {
        ShoppingCartVO shoppingCartVO = ShoppingCartDtoMapper.INSTANCE.do2VO(getById(id));
        return shoppingCartVO;
    }

    @Override
    public PageInfoTwo<ShoppingCartVO> pageQuery(ShoppingCartRequest request) {
        long count = shoppingCartMapper.pageCount(request);
        List<ShoppingCartDO> list = shoppingCartMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<ShoppingCartVO> shoppingCartVOList = ShoppingCartConvert.buildShoppingCartVoList(list);
        return new PageInfoTwo(request.getPageNum(), request.getPageSize(), shoppingCartVOList, count);
    }

    @Override
    public List<ShoppingCartVO> listQuery(ShoppingCartRequest request) {
        List<ShoppingCartDO> list = shoppingCartMapper.selectDOList(ShoppingCartDtoMapper.INSTANCE.request2DO(request));
        List<ShoppingCartVO> shoppingCartVOS = ShoppingCartConvert.buildShoppingCartVoList(list);
        return shoppingCartVOS;
    }

    @Override
    public Boolean add(ShoppingCartRequest request) {
        save(ShoppingCartDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    public Boolean updateByKey(ShoppingCartRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(ShoppingCartDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    public Boolean deleteByIds(String ids) {
        shoppingCartMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    @Transactional(rollbackFor =Exception.class)
    public Long addCar() {
        Long userId  = SessionContext.getWechatUserId();
        Long cartId = shoppingCartMapper.selectByUserId(userId);
        if (cartId==null){
            ShoppingCartDO shoppingCartDO = new ShoppingCartDO();
            shoppingCartDO.setUserId(userId);
            shoppingCartDO.setId(getId());
            shoppingCartMapper.insertDO(shoppingCartDO);
            return Long.valueOf(shoppingCartDO.getId());
        }else {
            return cartId;
        }
    }
}
